/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jigdo.downloader;

import java.util.ArrayList;
import java.util.logging.Logger;

/**
 * Thread object that handles downloads of iso. 
 * @author John R Sohn
 * @see DownloadPipe
 */
public class DownloadManager extends Thread implements ImageDownloadListener{
   
    Logger logger = Logger.getLogger(this.getClass().getName());
    
    public void setParentLogger(Logger parent)
    {
        logger.setParent(parent);
        logger.setLevel(null);
    }
    
    private Object locker = new Object();
    
    boolean running = false;
    
    /**
     * Main method that manages downloads of images.
     */
     @Override
        public void run() {
         
         while (running)
         {  
             
             
             
             synchronized(locker)
             {
                 try
                 {
                 locker.wait();
                 }
                 catch (Exception e)
                 {
                    
                 }
             }
         }
         
     }
     
     
     /**
     * The number of total available slots.
     */
   public int AvailableSlots;
   
   public int UsedSlots =0;
   
   public ArrayList<DownloadEntry> runningDownloads =  new ArrayList<DownloadEntry>();
   public ArrayList<ImageDownloadTask> Tasks = new ArrayList<ImageDownloadTask>(); 

   /**
    * Constructor specifying the initial number of download slots that can run concurrently.
    * @param slots 
    */
   public DownloadManager(int slots )
   {
       AvailableSlots = slots;
   }
   
  
   /**
    * Adds a new jigdo file to the download queue, and modifies the threadstate as needed.
    * @param Filename The filepath of the .jigdo file.
    * @throws Exception Occurs if the file could not be read, was not in the proper format, or was corrupt.
    */
   public void AddJigdoTask(String Filename) throws Exception
   {
       
       ImageDownloadTask image = new ImageDownloadTask(Filename);
       
       synchronized(Tasks)
       {
           image.Priority = DownloadPriority.Normal;
           Tasks.add(image);
           
          
       }
       
       image.AddListener(this);
       image.Initialize();
       
       this.Recalculate();
   }
   
   /**
    * Forces a manual rearrangement of running downloads using the rules provided to
    * stop/start/queue and dequeue waiting image download tasks.
    */
   public void Recalculate()
   {
      ArrayList<ImageDownloadTask> Critical =  GetByPriority(DownloadPriority.Critical);
      
      
      
      
   }
   
   
   
   /**
    * Queries the list of tasks currently downloading 
    * @param priority
    * @return 
    */
   public ArrayList<ImageDownloadTask> GetByPriority(DownloadPriority priority)
   {
       
       ArrayList<ImageDownloadTask> result = new ArrayList<ImageDownloadTask>();
       
       synchronized(Tasks)
       {
           for (int x=0; x < Tasks.size(); x++)
           {
               if (Tasks.get(x).Priority == priority )
               {
                   result.add(Tasks.get(x));
               }
               
           }
       }
       
       return result;
   }

    @Override
    public void Progress(DownloadEntry entry, long bytesDownloaded) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void StateChanged(DownloadEntry entry, DownloadStatus changedState) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void Error(DownloadEntry entry, Throwable error) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void PriorityChange(ImageDownloadTask image, DownloadPriority priority) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void Error(ImageDownloadTask image, Throwable Error) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void Progress(ImageDownloadTask image, int Step, int Max) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void StatusChange(ImageDownloadTask image, ImageStatus status) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
   
    
}
